<?php
/**
 * @copyright        2016 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           TL <mengwb@opencart.cn>
 * @created          2016-11-22 9:12:00
 * @modified         2016-11-22 9:12:00
 */

class ControllerExtensionModuleSettingGd extends Controller
{
    private $error = array();

    public function index()
    {
        $this->load->language('extension/module/setting_gd');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('setting/setting');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting('gdconfig', $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));
        }

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_edit'] = $this->language->get('text_edit');
        $data['text_enabled'] = $this->language->get('text_enabled');
        $data['text_disabled'] = $this->language->get('text_disabled');
        $data['text_select'] = $this->language->get('text_select');
        $data['text_none'] = $this->language->get('text_none');
        $data['text_yes'] = $this->language->get('text_yes');
        $data['text_no'] = $this->language->get('text_no');
        $data['text_enable'] = $this->language->get('text_enable');
        $data['text_disable'] = $this->language->get('text_disable');
        
        $data['entry_icp'] = $this->language->get('entry_icp');
        $data['entry_unpaid_status'] = $this->language->get('entry_unpaid_status');
        $data['entry_paid_status'] = $this->language->get('entry_paid_status');
        $data['entry_cancelled_status'] = $this->language->get('entry_cancelled_status');
        $data['entry_complete_time'] = $this->language->get('entry_complete_time');
        $data['entry_unpaid_time'] = $this->language->get('entry_unpaid_time');
        $data['entry_shipped_status'] = $this->language->get('entry_shipped_status');
        $data['entry_country_status'] = $this->language->get('entry_country_status');
        $data['entry_shipping_custom_field_telephone_id'] = $this->language->get('entry_shipping_custom_field_telephone_id');
        $data['entry_admin_telephone'] = $this->language->get('entry_admin_telephone');
        $data['help_shipping_custom_field_telephone_id'] = $this->language->get('help_shipping_custom_field_telephone_id');
        $data['help_icp'] = $this->language->get('help_icp');
        $data['help_unpaid_status'] = $this->language->get('help_unpaid_status');
        $data['help_paid_status'] = $this->language->get('help_paid_status');
        $data['help_cancelled_status'] = $this->language->get('help_cancelled_status');
        $data['help_complete_time'] = $this->language->get('help_complete_time');
        $data['help_unpaid_time'] = $this->language->get('help_unpaid_time');
        $data['help_shipped_status'] = $this->language->get('help_shipped_status');

        $data['entry_pc_payment'] = $this->language->get('entry_pc_payment');
        $data['help_pc_payment'] = $this->language->get('help_pc_payment');
        $data['entry_mobile_payment'] = $this->language->get('entry_mobile_payment');
        $data['help_mobile_payment'] = $this->language->get('help_mobile_payment');
        $data['entry_weixin_payment'] = $this->language->get('entry_weixin_payment');
        $data['help_weixin_payment'] = $this->language->get('help_weixin_payment');
        $data['entry_image_quick_upload_mode'] = $this->language->get('entry_image_quick_upload_mode');
        $data['help_image_quick_upload_mode'] = $this->language->get('help_image_quick_upload_mode');
        $data['help_country_status'] = $this->language->get('help_country_status');

        $data['button_save'] = $this->language->get('button_save');
        $data['button_cancel'] = $this->language->get('button_cancel');

        $data['tab_general'] = $this->language->get('tab_general');
        $data['tab_store'] = $this->language->get('tab_store');
        $data['tab_local'] = $this->language->get('tab_local');
        $data['tab_option'] = $this->language->get('tab_option');
        $data['tab_image'] = $this->language->get('tab_image');
        $data['tab_ftp'] = $this->language->get('tab_ftp');
        $data['tab_mail'] = $this->language->get('tab_mail');
        $data['tab_server'] = $this->language->get('tab_server');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['paid_status'])) {
            $data['error_paid_status'] = $this->error['paid_status'];
        } else {
            $data['error_paid_status'] = '';
        }

        if (isset($this->error['pc_payment'])) {
            $data['error_pc_payment'] = $this->error['pc_payment'];
        } else {
            $data['error_pc_payment'] = '';
        }
        if (isset($this->error['mobile_payment'])) {
            $data['error_mobile_payment'] = $this->error['mobile_payment'];
        } else {
            $data['error_mobile_payment'] = '';
        }
        if (isset($this->error['weixin_payment'])) {
            $data['error_weixin_payment'] = $this->error['weixin_payment'];
        } else {
            $data['error_weixin_payment'] = '';
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_stores'),
            'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('setting/setting_gd', 'token=' . $this->session->data['token'], true)
        );

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        $data['action'] = $this->url->link('extension/module/setting_gd', 'token=' . $this->session->data['token'], true);

        $data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);

        $data['token'] = $this->session->data['token'];

        if (isset($this->request->post['gdconfig_icp'])) {
            $data['gdconfig_icp'] = $this->request->post['gdconfig_icp'];
        } elseif ($this->config->get('gdconfig_icp')) {
            $data['gdconfig_icp'] = $this->config->get('gdconfig_icp');
        } else {
            $data['gdconfig_icp'] = '';
        }

        if (isset($this->request->post['gdconfig_shipping_custom_field_telephone_id'])) {
            $data['gdconfig_shipping_custom_field_telephone_id'] = $this->request->post['gdconfig_shipping_custom_field_telephone_id'];
        } elseif ($this->config->get('gdconfig_shipping_custom_field_telephone_id')) {
            $data['gdconfig_shipping_custom_field_telephone_id'] = $this->config->get('gdconfig_shipping_custom_field_telephone_id');
        } else {
            $data['gdconfig_shipping_custom_field_telephone_id'] = '';
        }

        if (isset($this->request->post['gdconfig_admin_telephone'])) {
            $data['gdconfig_admin_telephone'] = $this->request->post['gdconfig_admin_telephone'];
        } elseif ($this->config->get('gdconfig_admin_telephone')) {
            $data['gdconfig_admin_telephone'] = $this->config->get('gdconfig_admin_telephone');
        } else {
            $data['gdconfig_admin_telephone'] = 0;
        }

        if (isset($this->request->post['gdconfig_pc_payment'])) {
            $data['gdconfig_pc_payment'] = $this->request->post['gdconfig_pc_payment'];
        } elseif ($this->config->get('gdconfig_pc_payment')) {
            $data['gdconfig_pc_payment'] = $this->config->get('gdconfig_pc_payment');
        } else {
            $data['gdconfig_pc_payment'] = array();
        }

        if (isset($this->request->post['gdconfig_mobile_payment'])) {
            $data['gdconfig_mobile_payment'] = $this->request->post['gdconfig_mobile_payment'];
        } elseif ($this->config->get('gdconfig_mobile_payment')) {
            $data['gdconfig_mobile_payment'] = $this->config->get('gdconfig_mobile_payment');
        } else {
            $data['gdconfig_mobile_payment'] = array();
        }

        if (isset($this->request->post['gdconfig_weixin_payment'])) {
            $data['gdconfig_weixin_payment'] = $this->request->post['gdconfig_weixin_payment'];
        } elseif ($this->config->get('gdconfig_weixin_payment')) {
            $data['gdconfig_weixin_payment'] = $this->config->get('gdconfig_weixin_payment');
        } else {
            $data['gdconfig_weixin_payment'] = array();
        }

        if (isset($this->request->post['gdconfig_unpaid_status'])) {
            $data['gdconfig_unpaid_status'] = $this->request->post['gdconfig_unpaid_status'];
        } elseif ($this->config->get('gdconfig_unpaid_status')) {
            $data['gdconfig_unpaid_status'] = $this->config->get('gdconfig_unpaid_status');
        } else {
            $data['gdconfig_unpaid_status'] = 0;
        }

        if (isset($this->request->post['gdconfig_paid_status'])) {
            $data['gdconfig_paid_status'] = $this->request->post['gdconfig_paid_status'];
        } elseif ($this->config->get('gdconfig_paid_status')) {
            $data['gdconfig_paid_status'] = $this->config->get('gdconfig_paid_status');
        } else {
            $data['gdconfig_paid_status'] = array();
        }

        if (isset($this->request->post['gdconfig_cancelled_status'])) {
            $data['gdconfig_cancelled_status'] = $this->request->post['gdconfig_cancelled_status'];
        } elseif ($this->config->get('gdconfig_cancelled_status')) {
            $data['gdconfig_cancelled_status'] = $this->config->get('gdconfig_cancelled_status');
        } else {
            $data['gdconfig_cancelled_status'] = 0;
        }

        if (isset($this->request->post['gdconfig_complete_time'])) {
            $data['gdconfig_complete_time'] = $this->request->post['gdconfig_complete_time'];
        } elseif ($this->config->get('gdconfig_complete_time')) {
            $data['gdconfig_complete_time'] = $this->config->get('gdconfig_complete_time');
        } else {
            $data['gdconfig_complete_time'] = 0;
        }

        if (isset($this->request->post['gdconfig_unpaid_time'])) {
            $data['gdconfig_unpaid_time'] = $this->request->post['gdconfig_unpaid_time'];
        } elseif ($this->config->get('gdconfig_unpaid_time')) {
            $data['gdconfig_unpaid_time'] = $this->config->get('gdconfig_unpaid_time');
        } else {
            $data['gdconfig_unpaid_time'] = 0;
        }

        if (isset($this->request->post['gdconfig_shipped_status_id'])) {
            $data['gdconfig_shipped_status_id'] = $this->request->post['gdconfig_shipped_status_id'];
        } else {
            $data['gdconfig_shipped_status_id'] = $this->config->get('gdconfig_shipped_status_id');
        }

        if (isset($this->request->post['gdconfig_image_quick_upload_mode'])) {
            $data['gdconfig_image_quick_upload_mode'] = $this->request->post['gdconfig_image_quick_upload_mode'];
        } else {
            $data['gdconfig_image_quick_upload_mode'] = $this->config->get('gdconfig_image_quick_upload_mode');
        }

        if (isset($this->request->post['gdconfig_country_status'])) {
            $data['gdconfig_country_status'] = $this->request->post['gdconfig_country_status'];
        } else {
            $data['gdconfig_country_status'] = $this->config->get('gdconfig_country_status');
        }

        $this->load->model('extension/extension');

        $extensions = $this->model_extension_extension->getInstalled('payment');

        foreach ($extensions as $key => $value) {
            if (!is_file(DIR_APPLICATION . 'controller/extension/payment/' . $value . '.php') && !is_file(DIR_APPLICATION . 'controller/payment/' . $value . '.php')) {
                $this->model_extension_extension->uninstall('payment', $value);

                unset($extensions[$key]);
            }
        }

        $data['payments'] = array();

        $files = glob(DIR_APPLICATION . 'controller/{extension/payment,payment}/*.php', GLOB_BRACE);

        if ($files) {
            foreach ($files as $file) {
                $extension = basename($file, '.php');
                if(in_array($extension, $extensions) && $this->config->get($extension . '_status')){
                    $this->load->language('extension/payment/' . $extension);
    
                    $data['payments'][] = array(
                        'title'       => $this->language->get('heading_title'),
                        'code'     => $extension
                    );
                }
            }
        }

        $this->load->model('localisation/order_status');

        $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        $this->load->model('customer/custom_field');

        $data['custom_fields'] = $this->model_customer_custom_field->getCustomFields();

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('extension/module/setting_gd', $data));
    }

    protected function validate()
    {
        if (!$this->user->hasPermission('modify', 'extension/module/setting_gd')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }


        if (!isset($this->request->post['gdconfig_pc_payment'])) {
            $this->error['pc_payment'] = $this->language->get('error_pc_payment');
        }

        if (!isset($this->request->post['gdconfig_mobile_payment'])) {
            $this->error['mobile_payment'] = $this->language->get('error_mobile_payment');
        }

        if (!isset($this->request->post['gdconfig_weixin_payment'])) {
            $this->error['weixin_payment'] = $this->language->get('error_weixin_payment');
        }

        if (!isset($this->request->post['gdconfig_paid_status'])) {
            $this->error['paid_status'] = $this->language->get('error_paid_status');
        }


        return !$this->error;
    }

    public function template()
    {
        if ($this->request->server['HTTPS']) {
            $server = HTTPS_CATALOG;
        } else {
            $server = HTTP_CATALOG;
        }

        if (is_file(DIR_IMAGE . 'templates/' . basename($this->request->get['template']) . '.png')) {
            $this->response->setOutput($server . 'image/templates/' . basename($this->request->get['template']) . '.png');
        } else {
            $this->response->setOutput($server . 'image/no_image.png');
        }
    }
}
